/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Random;

/**
 * 简单的文本消息对象
 *
 * @author Eugene-Forest
 */
public class MessageModel implements Serializable {

    private static final long serialVersionUID = -7986924513844480481L;

    private Message_Type type =Message_Type.Default;

    private String messageString = "";

    private String publishDate = "";

    /**
     * 是否作为实时公告的标记，默认（false）作为一般信息对象
     */
    private boolean real_time = false;

    /**
     * 实时公告校验码
     */
    private String auth_code = "";


    /**
     * 创建连接端口消息对象
     * @param type 消息类型
     * @param messageString 消息
     */
    public MessageModel(Message_Type type, String messageString) {
        this.type = type;
        this.messageString = messageString;
    }

    /**
     * 创建普通的公告
     * @param type 消息类型
     * @param messageString 消息
     * @param publishDate 时间
     */
    public MessageModel(Message_Type type, String messageString, String publishDate) {
        this.type = type;
        this.messageString = messageString;
        this.publishDate = publishDate;
    }

    /**
     * 拷贝另一个MessageModel对象的数据
     * @param mm 另一个MessageModel对象
     */
    public MessageModel(MessageModel mm) {
        this.messageString = mm.messageString;
        this.publishDate = mm.publishDate;
        this.real_time = mm.real_time;
        this.type = mm.type;
        this.auth_code=mm.auth_code;
    }

    /**
     * 创建公告
     * @param type 消息类型
     * @param messageString 消息
     * @param publishDate 时间
     * @param real_time 是否是实时公告
     */
    public MessageModel(Message_Type type, String messageString, String publishDate, boolean real_time) {
        this.type = type;
        this.messageString = messageString;
        this.publishDate = publishDate;
        this.real_time = real_time;
        if (real_time) {
            set_auth_code();
        }
    }

    /**
     * 创建一个只带有指定类型的消息对象
     * @param type 消息类型
     */
    public MessageModel(Message_Type type) {
        this.type = type;
    }

    public MessageModel() {

    }

    public Message_Type getType() {
        return type;
    }

    public void setType(Message_Type type) {
        this.type = type;
    }

    public String getMessageString() {
        return messageString;
    }

    public void setMessageString(String messageString) {
        this.messageString = messageString;
    }

    public String getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(String publishDate) {
        this.publishDate = publishDate;
    }

    public boolean isReal_time() {
        return real_time;
    }

    public void setReal_time(boolean real_time) {
        this.real_time = real_time;
    }

    @Override
    public String toString() {
        return "message type is " + type + ";publish data is" + publishDate + ";"
                + " message content is " + messageString + "; and real time is " + String.valueOf(real_time);
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        if (messageString!=null){
            out.writeUTF(messageString);
        }else {
            out.writeUTF("");
        }
        if (type!=null){
            out.writeUTF(type.toString());
        }else {
            out.writeUTF(Message_Type.Default.toString());
        }
        if (publishDate!=null){
            out.writeUTF(publishDate);
        }
        else {
            out.writeUTF("");
        }
        out.writeBoolean(real_time);
        if (auth_code!=null){
            out.writeUTF(auth_code);
        }
        else {
            out.writeUTF("");
        }
    }

    /**
     * readObject()的作用则是针对对象的读取，其原理与writeObject()方法相同。
      */
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        messageString = in.readUTF();
        switch (in.readUTF().trim()) {
            case "Notice":
                type = Message_Type.Notice;
                break;
            case "Finish":
                type = Message_Type.Finish;
                break;
            case "Review":
                type = Message_Type.Review;
                break;
            case "Calling_Port":
                type=Message_Type.Calling_Port;
                break;
            case "Finish_Quit":
                type = Message_Type.Finish_Quit;
                break;
            case "Quit":
                type = Message_Type.Quit;
                break;
            default:
                type = Message_Type.Default;
                break;
        }
        publishDate = in.readUTF();
        real_time = in.readBoolean();
        auth_code=in.readUTF();
    }

    /**
     * 生成随机验证码，并保存入 auth_code 属性中
     */
    private void set_auth_code() {
        int maxNum = 36;
        int i;
        int count = 0;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < 8) {
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        auth_code=pwd.toString();
    }

    /**
     * 获取验证码
     * @return 验证码（8位字符组成的字符串）
     */
    public String get_auth_code(){
        return auth_code;
    }

    /**
     * 设置验证码
     * @param codeString 验证码字符串
     */
    public void set_auth_code(String codeString){
        this.auth_code=codeString;
    }
}
